package com.Hwadee.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.Hwadee.eduservice.client.VodClient;
import com.Hwadee.eduservice.entity.EduVideo;
import com.Hwadee.eduservice.mapper.EduVideoMapper;
import com.Hwadee.eduservice.service.EduVideoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程视频 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-06-29
 */
@Service
public class EduVideoServiceImpl extends ServiceImpl<EduVideoMapper, EduVideo> implements EduVideoService {

    @Autowired
    private VodClient vodClient;
    //删除小节及阿里云视频
    @Override
    public void removeByCourseId(String courseId) {
        //1.根据课程id查询所有的视频id
        QueryWrapper<EduVideo> wrapper1 = new QueryWrapper<>();
        wrapper1.select("video_source_id")
                .eq("course_id", courseId);
        List<EduVideo> eduVideos = baseMapper.selectList(wrapper1);

        //参数需要的List集合
        List<String> videoList = new ArrayList<>();
        //List<EduVideo>转换成List<String>
        for (int i = 0; i < eduVideos.size(); i++) {
                EduVideo eduVideo = eduVideos.get(i);
                String videoSourceId = eduVideo.getVideoSourceId();
                if (!StringUtils.isEmpty(videoSourceId)) {
                    //放到list集合中
                    videoList.add(videoSourceId);
                }
        }
        if (videoList.size() > 0) {
            //调用删除
            vodClient.deleteBatch(videoList);
        }

        QueryWrapper<EduVideo> wrapper2 = new QueryWrapper<>();
        wrapper2.eq("course_id", courseId);
        baseMapper.delete(wrapper2);
    }
}
